# This file is part of the replication packet for "A Low-Cost Information Nudge Increases Citizenship Application Rates Among Low-Income Immigrants"

# this script takes a text file of effect sizes that is created by the analysis file and creates the figure seen in the paper

# feewaivereffects.txt is created by the analysis.DO file

# Input
#     feewaivereffects.txt 
# 
# Output
#     figFeeWaiver.pdf

# This script recreaates the following figure in the paper
# Fig. 2: Effects of Voucher on Naturalization Application Rates Among Low-Income Im- migrants. 


# loading the packages ----------------------------------------------------


rm(list=ls())
library(ggplot2)
library(ggthemes)
library(RColorBrewer)
library(gridExtra)


# reading the data --------------------------------------------------------

### NEED TO CHANGE DATA LOCATIONS

# set location of the STATA output
# this will also be the location for the figure output
data_location <- ""

# set the directory of the data
setwd(data_location)

# load in the data
d <- read.table("feewaivereffects.txt",sep="\t",header=TRUE,row.names = 1)

# adding a row for the column names
d$name <- rownames(d)
d <- d[,c("pe","se","N","name")]


# name the figure output
figname <- c("figFeeWaiver.pdf")

# adding upper and lower bounds for the estimates
d$lb <- d$pe-1.96*d$se
d$ub <- d$pe+1.96*d$se

# reviwing dataframe d
d
# the first two rows of d are the application rates for the treated and control groups
# all other rows of d have an effect size that will be used to make an effects plot


# creating the top of figure 2 --------------------------------------------
# A bar chart that shows the application rate for the treated and control units

# selecting the application rates for treated and control with no covariates included
d1 <- d[1:2,]
d1

# hard code in the numbers from the responses
# 935 responded
# 200 did not receive the notice
# 735 did receive the notice

# renaming the name column for d1
d1$name <- factor(d1$name,levels=unique(d1$name)[2:1],labels=c("  Referral-Only Notice \n(N=200)","  Fee Waiver Notice\n(N=735)")[2:1])

# ggplot of d1
p <- ggplot(data = d1, aes(x = name, y = pe, fill = name,ymax=lb,ymin=ub)) + ggtitle("Application Rates in Treatment and Control Group")
p <- p + geom_bar(stat = "identity",position = "dodge",width=0.35) + 
  geom_errorbar(position = "dodge",width=0.15) +  
  coord_flip()
p <- p + scale_fill_manual(values=c("lightseagreen","indianred1")[2:1],guide = FALSE)  
p <- p + scale_y_continuous(name="Applied for Naturalization (%)",breaks=seq(0,90,10), 
                            labels=seq(0,90,10)        , limits = c(0,40))
p <- p + theme(axis.title.y = element_blank(),
               axis.title=element_text(size=12,face="bold"),
               axis.text=element_text(size=12,face="bold"),
               title=element_text(size=14,face="bold"),plot.margin = unit(c(.5,.5,.5,.5),"cm"))
p1 <- p
p1


# creating the bottom of Figure 2 ------------------------------------------------------
# Figure that includes covariates and subsample effects

d1 <- d[3:(nrow(d)-3),]
d1

# creating labels for the figure
d1$name <- factor(d1$name,levels=unique(d1$name)[length(unique(d1$name)):1],
                  labels=c("Full Sample\n(N=935)",
                           "Covariate Adjusted\n(Limited Set, N=935)",
                           "Covariate Adjusted\n(Extensive Set, N=935)",
                           "Male\n(N=345)",
                           "Female\n(N=590)",
                           "Less Than High School\n(N=167)",
                           "High School or Less\n(N=431)",
                           "Some College or Higher\n(N=504)",
                           "Registration in English\n(N=632)",
                           "Registration in Spanish\n(N=231)",
                           "HH Income Below Median\n(N=456)",
                           "HH Income Above Median\n(N=479)",
                           "Age Below Median\n(N=510)",
                           "Age Above Median\n(N=425)"
                  )[length(unique(d1$name)):1]
)

# ggplot code
p <- ggplot(data = d1, aes(x = name, y = pe, fill = "cornflowerblue",ymax=lb,ymin=ub)) + 
  ggtitle("Effect of Fee Waiver Notice")
p <- p + geom_point(pch=21,size=4,col="cornflowerblue",fill="cornflowerblue") + 
  geom_errorbar(position = "dodge",width=0,size=1.5,col="cornflowerblue") + 
  coord_flip()
p <- p +  ylim(-10,40) 
p <- p + ylab("Change in Naturalization Rate\n(percentage points)") + geom_hline(yintercept=0,col="slategrey",lty=2)
p2 <- p + theme(axis.title.y = element_blank(),
                axis.title=element_text(size=12,face="bold"),
                axis.text=element_text(size=12,face="bold"),
                title=element_text(size=14,face="bold"))
p2


# printing the two figures together ---------------------------------------

try(dev.off())
pdf(paste(figname), width = 9, height=10)
lay <- rbind(c(1),c(1),
             c(2),c(2),c(2),c(2),c(2)
)

grid.arrange(p1,p2,layout_matrix = lay)
dev.off()